﻿using IntoTheVoid.Models;
using System.ComponentModel;
using System.Diagnostics;
using System;
using System.IO;

namespace IntoTheVoid.State
{
    internal class UpdateState : State
    {
        public UpdateState(StateContext context)
        {
            buttonContent = "更新游戏";
            buttonEnabla = true;
            Context = context;
			Context.OnButtonStateArgs(new ButtonStateChangeEventArgs(buttonContent, buttonEnabla, false, true, "StartDownloadButton"));
		}
		public override void Start(StateContext context)
        {
			try
			{
				KillStartedGame();

			}catch (Exception ex)
			{

			}
			// 暂时切换下载游戏
			// Context.StateType = StateType.Downloading;
			Context.CurState = new DownloadState(Context);
			Context.CurState.Start(Context);
		}

		private void KillStartedGame()
		{
			foreach (Process p in Process.GetProcesses())
			{
				if (p.ProcessName.Contains("IntoTheVoid"))
				{
					string currentProcessPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
					if (p.MainModule.FileName == currentProcessPath)
					{
						continue;
					}
					try
					{
						p.Kill();
						break;
					}
					catch (Win32Exception)
					{
					}
				}
			}
		}
	}
}
